﻿<%@ WebHandler Language="C#" Class="UploadHandler" %>

using System;
using System.Web;
using System.IO;
using DeviceInfoManage.BusinessLogicLayer;

public class UploadHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Charset = "utf-8";

        HttpPostedFile file = context.Request.Files["Filedata"];
        string uploadPath =
            HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\";
        uploadPath = HttpUtility.UrlDecode(uploadPath);
        string type = HttpUtility.UrlDecode(context.Request.QueryString["type"]);
        string owner = context.Request.QueryString["owner"];
        string folder = HttpUtility.UrlDecode(context.Request["folder"]);
        string path = folder + "/" + file.FileName;
        if (file != null)
        {
            string[] strSplit = file.FileName.Split('.');
            int lth = strSplit[strSplit.Length - 1].Length + 1;
            string fileName = file.FileName.Remove(file.FileName.Length - lth);
            if (!Directory.Exists(uploadPath))
            {
                Directory.CreateDirectory(uploadPath);
            }

            if (File.Exists(uploadPath + file.FileName))
            {
                //File.Delete(uploadPath + file.FileName);
                //File.Move(uploadPath + file.FileName, uploadPath + "[备份]" + file.FileName);
                //context.Response.Write("保存失败,存在同名文件!");
                context.Response.Write("0");
            }
            else
            {
                FileManage fm = new FileManage();
                fm.fileName = fileName;
                fm.fileType = type;
                fm.uploadUser = owner;
                fm.uploadTime = DateTime.Now;
                fm.filePath = folder + "/" + file.FileName;
                int i = fm.Add(fm);

                file.SaveAs(uploadPath + file.FileName);

                //下面这句代码缺少的话，上传成功后上传队列的显示不会自动消失
                context.Response.Write("1");
            }

        }
        else
        {
            context.Response.Write("0");
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}